package com.ruoyi.web.controller.V2.inter.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.V2.inter.service.ISalePartnerService;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.core.OutUrlConstant;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 销售伙伴Controller
 *
 * @author ruoyi
 * @date 2022-06-07
 */
@RestController
@RequestMapping("/error/problem")
public class ErrorProblemController extends BaseController
{
    @Autowired
    private ISalePartnerService salePartnerService;

    //查询盼之游戏列表
    @GetMapping("/gameList")
    public String gameList(){
        String s = pzzGameList();
        String data = JSONObject.parseObject(s).getString("data");
        return data;
    }

    //查询盼之店铺
    @GetMapping("/salepartnerList")
    public TableDataInfo list(String name){
        SalePartner salePartner = new SalePartner();
        salePartner.setName(name);
        List<SalePartner> salePartnerList = salePartnerService.selectSalePartnerList(salePartner);
        return getDataTable(salePartnerList);
    }

    /**
     * 查询销售伙伴列表
     */
    @GetMapping("/pzzError")
    public Map<String,Object> list(String goodsNo,String gameId,long saleId)
    {
        Map<String,Object> params = new HashMap<>();
        SalePartner salePartner = salePartnerService.selectSalePartnerById(saleId);
        String s = selectError(goodsNo, gameId, salePartner);
        params.put("code",200);
        params.put("msg",s);
        return params;
    }

    static String pzzGameList(){
        Map<String, Object> action = new TreeMap<>();
//        action.put("goodsCatalogueId", 6);
//        action.put("gameId", 391);
        action.put("proxyId", "18093522409");
        Map<String, Object> params = new HashMap<>();
        params.put("proxyId", "18093522409");
        params.put("action", action);
        params.put("timestamp", 1658153198368L + "");

        String proxyKey = "pzds_open_api_sign_A536AF99048D6484650091B84CD48FA9";
//        String proxyKey = "pzds_open_api_sign_3430E933C35AD3AC1820D23485CBE64E";

        String cover = getCover(action) + "timestamp=" + params.get("timestamp").toString() + "&key=" + proxyKey;
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign", sign);
        String response = "";
        response = HttpUtils.sendPost(OutUrlConstant.PANZHIURL + "/api/openData/cooperateGameGoods", JSONObject.toJSONString(params), "application/json");
        return response;
    }

    //盼之审核失败原因查询
    static String selectError(String outGoodsNo,String gameId,SalePartner salePartner){
        Map<String, Object> action = new TreeMap<>();
        action.put("outGoodsNo", outGoodsNo);
        action.put("gameId", gameId);
//        action.put("serialVersionUID", 1);
        Map<String, Object> params = new HashMap<>();
        params.put("proxyId", salePartner.getOther());
        params.put("action", action);
        params.put("timestamp", System.currentTimeMillis() + "");

        String cover = getCover(action) + "timestamp=" + params.get("timestamp").toString() + "&key=" + salePartner.getSaleKey();
        System.out.println(cover);
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign", sign);
        String paramsStr = JSONObject.toJSONString(params);

        String response = HttpUtils.sendPost(OutUrlConstant.PANZHIURL + "/api/openData/goodsErrorInfo", paramsStr, "application/json");
        return response;
    }

    public static String getCover(Map<String, Object> params) {
        StringBuffer resut = new StringBuffer();
        for (String key : params.keySet()) {
            if (params.get(key) instanceof String && StringUtils.isNotEmpty((String) params.get(key))) {
                resut.append(key + "=" + params.get(key) + "&");
            }
            if (params.get(key) instanceof Boolean) {
                resut.append(key + "=" + params.get(key) + "&");
            }
            if (params.get(key) instanceof Long) {
                resut.append(key + "=" + params.get(key) + "&");
            }
            if (params.get(key) instanceof Integer && null != params.get(key)) {
                resut.append(key + "=" + params.get(key) + "&");
            }
        }
        return resut.toString();
    }

    /**
     * 查询销售伙伴列表
     */
    @GetMapping("/allList")
    public TableDataInfo allList(SalePartner salePartner)
    {
        List<SalePartner> list = salePartnerService.selectSalePartnerList(salePartner);
        return getDataTable(list);
    }

    /**
     * 导出销售伙伴列表
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:export')")
    @Log(title = "销售伙伴", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SalePartner salePartner)
    {
        List<SalePartner> list = salePartnerService.selectSalePartnerList(salePartner);
        ExcelUtil<SalePartner> util = new ExcelUtil<SalePartner>(SalePartner.class);
        return util.exportExcel(list, "销售伙伴数据");
    }

    /**
     * 获取销售伙伴详细信息
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(salePartnerService.selectSalePartnerById(id));
    }

    /**
     * 新增销售伙伴
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:add')")
    @Log(title = "销售伙伴", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SalePartner salePartner)
    {
        return toAjax(salePartnerService.insertSalePartner(salePartner));
    }

    /**
     * 修改销售伙伴
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:edit')")
    @Log(title = "销售伙伴", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SalePartner salePartner)
    {
        return toAjax(salePartnerService.updateSalePartner(salePartner));
    }

    /**
     * 删除销售伙伴
     */
    @PreAuthorize("@ss.hasPermi('partner:partner:remove')")
    @Log(title = "销售伙伴", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(salePartnerService.deleteSalePartnerByIds(ids));
    }
}
